<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\form\util\control;

/**
 * 穿梭框控件
 */
class Transfer extends Control
{
    /**
     * @var array
     */
    private $_options = [
        'control' => 'transfer',
        'label' => '',
        'default' => [],
        'required' => false,
        'comment' => '',
        'help' => '',
        'layout' => 12,
        'style' => '',
        'class' => '',
        'attribute' => '',
        '_setting' => [
            'title' => ['列表一', '列表二'],
            // [['title' => '标题', 'value'=> '值', 'checked' => true, 'disabled' => false], ...]
            'data' => [],
            //是否开启搜索。支持以下可选值：
            //false 不开启搜索（默认）
            //true 开启搜索，且匹配时不区分大小写
            //cs 开启搜索，且匹配时区分大小写 2.7+
            'showSearch' => false,
            'width' => 200,
            'height' => 360,
            'text' => [
                'none' => '无数据', // 没有数据时的文案
                'searchNone' => '无匹配数据', // 搜索无匹配数据时的文案
            ],
        ],
        'event_dblclick' => '',
    ];

    /**
     * ```php
     * event_dbClick(<<<JS
     *  // 上下文查看 builder/assets/ym2/ym2.bootstrap.js 599:63
     *  return function(obj){
     *             console.log(obj.elem); // 点击的元素
     *             console.log(obj.data); // 得到点击项的数据
     *             console.log(obj.index); // 如果数据来自左边，index 为 0，否则为 1
     *          return false // 返回 false 会阻止穿梭
     *  }
     * JS)
     * ```
     * @param string $jsCallbackFunc 双击时javascript回调函数
     * @see builder/assets/ym2/ym2.bootstrap.js 599:63
     * @return $this
     */
    public function event_dbClick($jsCallbackFunc)
    {
        $this->_options['event_dblclick'] = (string)$jsCallbackFunc;
        return $this;
    }

    /**
     * @param array $textOptions 自定义默认文本
     * @return $this
     */
    public function setting_text(array $textOptions)
    {
        $this->_options['_setting']['text'] = $textOptions;
        return $this;
    }

    /**
     * @param int $height 左右列表高度
     * @return $this
     */
    public function setting_height($height)
    {
        $this->_options['_setting']['height'] = (int)$height;
        return $this;
    }

    /**
     * @param int $width 左右列表宽度
     * @return $this
     */
    public function setting_width($width)
    {
        $this->_options['_setting']['width'] = (int)$width;
        return $this;
    }

    /**
     * @param bool $boolean 是否开启搜索
     * @return $this
     */
    public function setting_showSearch($boolean)
    {
        $this->_options['_setting']['showSearch'] = (boolean)$boolean;
        return $this;
    }

    /**
     * @param array $data 穿梭框数据选项
     * @return $this
     */
    public function setting_data(array $data)
    {
        $this->_options['_setting']['data'] = $data;
        return $this;
    }

    /**
     * @param array $titles 穿梭框左右列表标题
     * @return $this
     */
    public function setting_title(array $titles)
    {
        $this->_options['_setting']['title'] = $titles;
        return $this;
    }

    /**
     * 控件标签
     * @param string|null $label
     * @return $this
     */
    public function label($label)
    {
        $this->_options['label'] = $label;
        return $this;
    }

    /**
     * 默认值
     * @param array $default
     * @return $this
     */
    public function defaultValue(array $default = [])
    {
        $this->_options['default'] = (array)$default;
        return $this;
    }

    /**
     * 是否必填
     * @param bool $required
     * @return $this
     */
    public function required($required = true)
    {
        $this->_options['required'] = (boolean)$required;
        return $this;
    }

    /**
     * 字段注释
     * @param string $comment
     * @return $this
     */
    public function comment($comment)
    {
        $this->_options['comment'] = (string)$comment;
        return $this;
    }

    /**
     * 帮助文字
     * @param string $help
     * @return $this
     */
    public function help($help)
    {
        $this->_options['help'] = (string)$help;
        return $this;
    }

    /**
     * 布局，1-12
     * @param int $layout
     * @return $this
     */
    public function layout($layout)
    {
        $this->_options['layout'] = (int)$layout;
        return $this;
    }

    /**
     * CSS样式
     * @param array|string $style
     * @return $this
     */
    public function style($style)
    {
        $this->formatStyle($style);
        $this->_options['style'] = $style;

        return $this;
    }

    /**
     * UI
     * @param array|string $class
     * @return $this
     */
    public function uiClass($class)
    {
        $this->formatUiClass($class);
        $this->_options['class'] = $class;

        return $this;
    }

    /**
     * HTML属性
     * @param array|string $attribute
     * @return $this
     */
    public function attribute($attribute)
    {
        $this->formatHtmlAttribute($attribute);
        $this->_options['attribute'] = $attribute;

        return $this;
    }

    public function _toArray()
    {
        // TODO: Implement _toArray() method.
        return $this->_options;
    }
}